﻿using System.Collections.Generic;
using System.Linq;
using Foothill.DataAccess.Interface;
using Foothill.Model;

namespace Foothill.Service.AdService
{
    internal class AdminSearchService : IAdminSearchService
    {
        private readonly IAdSearchRepo _adSearchRepo;
        private readonly IPostValidator _postValidator;

        public AdminSearchService(IAdSearchRepo adSearchRepo, IPostValidator postValidator)
        {
            _postValidator = postValidator;
            _adSearchRepo = adSearchRepo;
        }
        
        public IList<ForSaleAd> GetPostsByIds(string commaSeperatedId)
        {
            if (string.IsNullOrWhiteSpace(commaSeperatedId))
            {
                return new List<ForSaleAd>();
            }

            string[] tokens = commaSeperatedId.Split(',');
            IList<int> idList = new List<int>();
            int tmpInt;
            foreach (string token in tokens)
            {
                if (_postValidator.IsNonNegativeNumber(token, out tmpInt))
                {
                    idList.Add(tmpInt);
                }
            }
            
            if (!idList.Any())
            {
                return new List<ForSaleAd>();
            }

            return _adSearchRepo.SearchByIdList(idList);
        }
    }
}
